/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.personalNotes.client;

import com.gadglet.client.gwt.core.GadgletQuery;
import com.gadglet.client.gwt.core.GadgletRequest;
import com.gadglet.gadgets.personalNotes.shared.Params;
import com.gadglet.params.FilterByBoolOpTypes;
import com.gadglet.params.FilterByParamTypes;
import com.gadglet.params.FilterByRelOpTypes;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * the functionality of Priority Selection when adding a new Personal note
 *
 */
public class PrioritySelection{
	
	private final HorizontalPanel prioritySelectionPanel = new HorizontalPanel();
	final PreferencesFeature  prefsUtils = PreferencesProvider.get();
	GadgletQuery localViewPersonalNotesQry ;
	GadgletRequest localViewPersonalNotesRequest ;
	
	PrioritySelection(GadgletQuery viewPersonalNotesQry,GadgletRequest viewPersonalNotesRequest){
	
		 localViewPersonalNotesQry = viewPersonalNotesQry;
		 localViewPersonalNotesRequest = viewPersonalNotesRequest;
		 RadioButton allPriority = new RadioButton("priority",prefsUtils.getMsg("gadgetLabelAll"));
		 RadioButton highPriority = new RadioButton("priority",prefsUtils.getMsg("gadgetLabelHigh"));
		 RadioButton midPriority =	new RadioButton("priority",prefsUtils.getMsg("gadgetLabelMedium"));
		 RadioButton lowPriority =  new RadioButton("priority",prefsUtils.getMsg("gadgetLabelLow"));
		 
		 prioritySelectionPanel.add(allPriority);
		 prioritySelectionPanel.add(highPriority);
		 prioritySelectionPanel.add(midPriority);
		 prioritySelectionPanel.add(lowPriority);
		 
		 allPriority.setValue(true);
		 
		 allPriority.addClickHandler(new ClickHandler(){@Override
		public void onClick(ClickEvent c){ 
			 getRequestQuery().removeFilterBy("priority",Params.PRIORITY.getParamName());
			 getPlatformRequest().makeRequest();
		 }});
		 highPriority.addClickHandler(new ClickHandler(){@Override
		public void onClick(ClickEvent c){ 
			
			 if(getRequestQuery().filterByExists("priority",Params.PRIORITY.getParamName()))
				 getRequestQuery().changeParamValue(Params.PRIORITY.getParamName(), "1");
			 else
				 getRequestQuery().addFilterBy(FilterByBoolOpTypes.AND, "priority", FilterByRelOpTypes.EQUAL, Params.PRIORITY.getParamName(), "1", FilterByParamTypes.INT);
			 getPlatformRequest().makeRequest();
		 }});
		 midPriority.addClickHandler(new ClickHandler(){@Override
		public void onClick(ClickEvent c){ 
			 if(getRequestQuery().filterByExists("priority",Params.PRIORITY.getParamName()))
				 getRequestQuery().changeParamValue(Params.PRIORITY.getParamName(), "2");
			 else
				 getRequestQuery().addFilterBy(FilterByBoolOpTypes.AND, "priority", FilterByRelOpTypes.EQUAL, Params.PRIORITY.getParamName(), "2", FilterByParamTypes.INT);
			 getPlatformRequest().makeRequest();
		 }});
		 lowPriority.addClickHandler(new ClickHandler(){@Override
		public void onClick(ClickEvent c){
			 if(getRequestQuery().filterByExists("priority",Params.PRIORITY.getParamName()))
				 getRequestQuery().changeParamValue(Params.PRIORITY.getParamName(), "3");
			 else
				 getRequestQuery().addFilterBy(FilterByBoolOpTypes.AND, "priority", FilterByRelOpTypes.EQUAL, Params.PRIORITY.getParamName(), "3", FilterByParamTypes.INT);
			 
			 getRequestQuery().setStartItemsFrom(0);
			 getPlatformRequest().makeRequest();
		 }});
		 
	}
	
	
	public GadgletQuery getRequestQuery(){
		return localViewPersonalNotesQry;
	}
	
	GadgletRequest getPlatformRequest(){
		return localViewPersonalNotesRequest ;
	}
	
	
	public HorizontalPanel getPanel(){
		return prioritySelectionPanel;
	}
	
		
}